package com.qiaotong.colorpicker;

import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.annotation.ColorInt;
import androidx.annotation.IntRange;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.qiaotong.colorpicker.widget.colorpicker.AlphaPicker;
import com.qiaotong.colorpicker.widget.colorpicker.CircleColorPicker;
import com.qiaotong.colorpicker.widget.colorpicker.ColorPicker;
import com.qiaotong.colorpicker.widget.colorpicker.GrayPicker;
import com.qiaotong.colorpicker.widget.colorpicker.OnValueChangeListener;

public class CircleActivity extends AppCompatActivity implements OnValueChangeListener {
    ViewGroup mMainContent;
    View mPreview;
    TextView mColorValue;
    TextView mAlphaValue;
    AlphaPicker mAlphaPicker;

    @IntRange(from = 0, to = 255)
    int mCurAlpha = 0xFF;
    @ColorInt
    int mCurColor = Color.RED;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_circle);
        mMainContent = findViewById(R.id.content_main);
        mPreview = findViewById(R.id.preview);
        mColorValue = findViewById(R.id.color_value);
        mAlphaValue = findViewById(R.id.alpha_value);

        CircleColorPicker circleColorPicker = findViewById(R.id.color_picker_circle);
        circleColorPicker.setOnValueChangeListener(this);



        initBarPickers();
        refreshPreviews(255, Color.RED);
    }


    void initBarPickers() {
        ColorPicker colorPicker = findViewById(R.id.color_picker);
        colorPicker.setColor(mCurColor);
        colorPicker.setOnValueChangeListener(this);

        mAlphaPicker = findViewById(R.id.alpha_picker);
        mAlphaPicker.setAlphaInt(mCurAlpha);
        mAlphaPicker.setOnValueChangeListener(this);

        GrayPicker grayPicker = findViewById(R.id.gray_picker);
        grayPicker.setOnValueChangeListener(this);
    }

    void refreshPreviews(int alpha, int color) {
        color = color & 0x00FFFFFF;
        if (color != mCurColor) {
            this.mCurColor = color;
            mAlphaPicker.setPreviewColor(color);
        }
        this.mCurAlpha = alpha;
        mPreview.setBackgroundColor((alpha << 24) | color);
        String string = getString(R.string.color_value, mCurColor);
        mColorValue.setText(string);
        string = getString(R.string.alpha_value, alpha);
        mAlphaValue.setText(string);
    }

    @Override
    public void onValueChanged(View view, int newValue) {
        if (view instanceof AlphaPicker) {
            refreshPreviews(newValue, mCurColor);
        } else {
            refreshPreviews(mCurAlpha, newValue);
            Color.red(newValue);
            Color.green(newValue);
            Color.blue(newValue);
//            Log.i("gao","newValue = "+newValue);
        }
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        View view = findViewById(R.id.horizontal_picker_container);
        mMainContent.removeViewAt(mMainContent.getChildCount() - 1);
        if (view == null) {
            getLayoutInflater().inflate(R.layout.bar_pickers_horizontal, mMainContent);
        } else {
            getLayoutInflater().inflate(R.layout.bar_pickers_vertical, mMainContent);
        }
        initBarPickers();
        refreshPreviews(mCurAlpha, mCurColor);
        return true;
    }
}